package com.dajingling.controller;

import com.dajingling.bean.*;
import com.dajingling.mvc.ResponseBody;
import com.dajingling.service.EUserExpressService;
import com.dajingling.util.DateFormatUtil;
import com.dajingling.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class EUserExpressController {
    @ResponseBody("/admin/views/console_e_user.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        Map<String,Integer> list = EUserExpressService.console();
        Message msg = new Message();
        if (list.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(list);
        String json = JSONUtil.toJSON(msg);
        System.out.println("控制台："+json);
        return json;
    }

    @ResponseBody("/admin/views/courier/findAll.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //1.获取查询数据的索引
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2.获取查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3.进行查询
        List<EUserExpress> list = EUserExpressService.findAll(true,offset,pageNumber);
        List<BootStrapTableUser> list1 = new ArrayList<>();

        for (EUserExpress e:list){
            String logonTime = DateFormatUtil.format(e.getLogonTime());
            String loginTime = e.getLoginTime()==null?"未登录":DateFormatUtil.format(e.getLoginTime());
            String expressNumber = String.valueOf(e.getExpressNumber());
            BootStrapTableUser e2 = new BootStrapTableUser(e.getUid(), e.getUserName(), e.getPassword(), e.getUserPhone(), e.getIdCard(), expressNumber, logonTime, loginTime, e.getLoginIp());
            list1.add(e2);
        }

        Map<String,Integer> console = EUserExpressService.console();
        int total = console.get("data_size");
        //4.把集合封装为bootstrap-table(前端的表格)能识别的格式
        ResultData<BootStrapTableUser> data = new ResultData<>();
        data.setRows(list1);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        System.out.println("快递员列表："+json);
        return json;
    }

    @ResponseBody("/admin/views/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String userName = request.getParameter("username");
        String userPhone = request.getParameter("userphone");
        String idCard = request.getParameter("idcard");
        String password = request.getParameter("password");
        EUserExpress e = new EUserExpress(userName,password,userPhone,idCard);
        boolean flag =  EUserExpressService.insert(e);
        Message msg = new Message();
        if (flag){
            //录入成功
            msg.setStatus(0);
            msg.setResult("快递员录入成功！");
        }else {
            msg.setStatus(-1);
            msg.setResult("快递员录入失败！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("快递员录入："+json);
        return json;
    }
    @ResponseBody("/admin/views/courier/findByUserPhone.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userphone");
        EUserExpress e = EUserExpressService.findByUserPhone(userPhone);
        Message msg = new Message();
        if (e == null){
            msg.setStatus(-1);
            msg.setResult("号码不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e);
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("查询快递员："+json);
        return json;
    }

    @ResponseBody("/admin/views/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        String oldUserPhone = request.getParameter("olduserphone");
        String userName = request.getParameter("username");
        String userPhone = request.getParameter("userphone");
        String idCard = request.getParameter("idcard");
        String password = request.getParameter("password");
        EUserExpress e = new EUserExpress(userName,password,userPhone,idCard);
        boolean flag = EUserExpressService.update(oldUserPhone,e);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("修改成功！");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改失败！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("修改快递员："+json);
        return json;
    }

    @ResponseBody("/admin/views/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userphone");
        boolean flag = EUserExpressService.delete(userPhone);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("删除成功！");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("删除快递："+json);
        return json;
    }
}